package com.vesper.mysdk

import android.util.Log
import androidx.annotation.IntDef

abstract class MSDK {

    companion object {
        val TAG: String = "MSDK"

        /**
         * 定一个常见的Reason注解，用于限制参数只能取@IntDef中声明的参数的其中一个
         */
        @Retention(AnnotationRetention.SOURCE)//指定这个注解只在源代码中保留，不会在编译后的字节码中出现
        @IntDef(
            REASON_NORMAL,
            REASON_NOT_INIT,
            REASON_NOT_LOGIN,
            REASON_ALREADY_LOGIN,
            MREASON_ACCOUNT_OR_PASSWORD_WRONG
        )
        //@IntDef 限制参数的取值
        annotation class MReason

        //正常原因
        const val REASON_NORMAL = 0

        //没有初始化
        const val REASON_NOT_INIT = 1

        //没有登录
        const val REASON_NOT_LOGIN = 2

        //重复登录
        const val REASON_ALREADY_LOGIN = 3

        //账号密码错误
        const val MREASON_ACCOUNT_OR_PASSWORD_WRONG = 4


        /**
         * 常见的错误
         */
        @Retention(AnnotationRetention.SOURCE)
        annotation class MError


        /**
         *登录的状态
         */
        @Retention(AnnotationRetention.SOURCE)
        @IntDef(STATUS_NOT_INIT, STATUS_INITED, STATUS_LOGINED, STATUS_NOT_LOGIN)
        annotation class SDKStatus

        //未初始化
        const val STATUS_NOT_INIT = 0

        //已初始化
        const val STATUS_INITED = 1

        //未登录
        const val STATUS_NOT_LOGIN = 2

        //已登录
        const val STATUS_LOGINED = 3


        var mSdk: MSDK? = null

        /**
         * 初始化SDK
         * @param secretKey secretKet
         * @param appKey appKey
         * @param callback sdk状态的回调
         */
        fun initSDK(secretKey: String, appKey: String, callback: MCallback): MSDK {
            if (mSdk != null && mSdk?.getLoginStatus() !== STATUS_NOT_INIT) {
                Log.e(TAG, "initSDK: SDK already initiated!")
                return mSdk!!
            } else {
                mSdk = MSDKImpl(secretKey, appKey, callback)
                return mSdk!!
            }
        }

        /**
         * 销毁SDK
         */
        fun destory() {
            if (mSdk != null) {
                mSdk?.destroySDK()
                mSdk = null
            }
        }

    }

    private fun MSDK.initialize() {
        println("123123")
    }

    /**
     * 获取当前的状态
     * 使用@MStatus标记getLoginStatus返回的值，声明该方法返回的是一个状态码
     */
    abstract @SDKStatus
    fun getLoginStatus(): Int

    /**
     * 登录方法
     *@param uid 账号
     * @param password 密码
     */
    abstract fun login(uid: String, password: String)


    /**
     * 登出
     */
    abstract fun logout()

    /**
     * 销毁sdk
     */
    abstract fun destroySDK()

}